﻿using Microsoft.Xna.Framework;

namespace DARE
{
    public class CTransitionBlender : IBlendController
    {
        #region Fields

        private readonly CAnimConfig m_up;
        private readonly CAnimConfig m_down;
        private double m_elapled;
        private double m_duration;

        #endregion

        #region Properties

        public int Priority { get; set; }

        #endregion

        #region Event

        public event BlendEvent BlendingEnded;

        #endregion

        #region CTOR

        public CTransitionBlender(CAnimConfig caUp, CAnimConfig caDown, double duration)
        {
            m_up = caUp;
            m_down = caDown;
            m_duration = duration;
        }

        #endregion

        #region Method

        public void Update(GameTime time)
        {
            if (m_elapled > m_duration)
                return;
            m_elapled += time.ElapsedGameTime.TotalMilliseconds;
            float percentage = MathHelper.Clamp((float)(m_elapled / m_duration), 0.0f, 1.0f);
            m_up.BlendFactor = percentage;
            m_down.BlendFactor = 1 - percentage;
            if (m_elapled > m_duration)
            {
                m_down.State = EAnimState.STOP;
                if (BlendingEnded != null)
                    BlendingEnded.Invoke(this);
            }

        }

        #endregion

    }
}
